<?php

!defined('IN_ROOT') && exit('Access Denied');

class linecontrol extends base {

    function linecontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load("line");
        $this->load('area');
        $this->load('cate');
        $this->load('sysmap');
    }

    /*前台查看公告列表*/
    function onlist() {
    	if (isset($this->post['submit'])) {
    		$data = $this->post['data'];
    		$queryString = http_build_query($data);
    		redirect('index.php?line/list/'.$data['title'].'/'.$data['cateid']);
    	}
    	$title = $this->get['2'];
    	$cateid = $this->get['3'];
    	$where = '';
    	if (!empty($title)) {
    		$where .= " AND `title` LIKE '%$title%'";
    	} 
    	if (!empty($cateid)) {
    		$where .= " AND `cateid`='$cateid'";
    	}     	
        $navtitle='公告列表';
        @$page = max(1, intval($this->get[2]));
        $pagesize = $this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $rownum=$this->db->fetch_total('line',' 1=1'.$where);
        $uid = $this->user['uid'];
        $linelist = $_ENV['line']->get_list($startindex,$pagesize, " `uid`='$uid'$where");
        $departstr=page($rownum, $pagesize, $page,"line/list/$title/$cateid");//得到分页字符串
        $catelist = $_ENV['line']->getcate($uid);
        include template('linelist');
    }
    
    function ondelete()
    {
    	$lid = $this->get[2];
    	if (!empty($lid)) {
    		$uid = $this->user['uid'];
    		$info = $_ENV['line']->get($lid);
    		if ($uid == $info['uid']) {
    			$_ENV['line']->remove_by_id($lid);
    			msg('删除成功！');
    		} else {
    			msg('非法操作！');
    		}
    	}
    }

    /*批发商添加线路*/
    function onpifaadd() {
        $line=$_ENV['line']->get($this->get[2]);
        $line['time']=tdate($line['time'],3,0);
        include template('line');
    }
    function onlinecateadd()
    {
    	$uid = $this->user['uid'];
    	if (isset($this->post['submit'])) {
    		$catename = $this->post['catename'];
    		$data = array(
    			'catename' => $catename,
    			'uid' => $uid
    		);
    		$insert_id = $_ENV['line']->addcate($data);
    		if ($insert_id) {
    			redirect('?line/linecateadd');
    		} else {
    			msg("添加失败，请重试！", "?line/linecateadd");
    		}
    	}
    	$list = $_ENV['line']->getcate($uid);
    	include template('linecateadd');
    }
    function onlinecatedelete()
    {
    	$cate_id = $this->get[2];
    	if ($cate_id) {
    		$cateOne = $_ENV['line']->getcateone($cate_id);
    		if ($cateOne['uid'] == $this->user['uid']) {
    			$_ENV['line']->deletecate($cate_id);
    			redirect("?line/linecateadd");
    		} else {
    			msg("非法操作！", "?line/linecateadd");
    		}
    	} else {
    		msg("ID不能为空！", "?line/linecateadd");
    	}
    }
	function onpifaline() {
		$uid = $this->user['uid'];
		$randString = randString();
		if(isset($this->post['uid'])) {
            $idcode = $this->post['idcode'];
            $title = $this->post['title'];
            $topid = $this->post['topid'];
            foreach ($topid as $tk => $tv) {
            	$cate_{$tk} = $this->post["cate_$tk"];
            	if(count($cate_{$tk}) ==0){
            		msg($tv."不能为空");
            	}
            }
            
            //$siteid = (int)$this->post['siteid'];
            //$siteid == 0 && msg("请选择分站");
            //$fenleiid = (int)$this->post['fenleiid'];
            //$fenleiid == 0 && msg("请选择分类");
            //$leibieid = (int)$this->post['leibieid'];
            //$leibieid == 0 && msg("请选择类别");
            //$pifasite = $this->post['pifasite'];
            //count($pifasite) ==0 && msg("请选择发布的分站");
            //$pifaarea = $this->post['pifaarea'];
            //count($pifaarea) ==0 && msg("请选择发布到的地区");
            $cates = $this->post['cates'];
            count($cates) ==0 && msg("请选择发布到的地区");
            !$title && msg("线路名称不能为空");
            $cateid = $this->post['cate_id'];
            //$cateconfig = (int)$this->post['cateconfig'];
            //$cateconfig == 0 && msg("请选择线路类别");
            $views = $this->post['viewshow'];
            //count($views) ==0 && msg("请选择途经景点");
            $earlier_date = (int)$this->post['earlier_date'];
            $earlier_date == 0 && msg("提前报名的天数不能为空");
            $daynum = (int)$this->post['daynum'];
            $daynum == 0 && msg("行程天数不能为空");
            $group_date = (int)$this->post['group_date'];
            $traffic = $this->post['traffic'];
            !$traffic && msg("交通方式不能为空");
            $orderid = (int)$this->post['orderid'];
            $isrec = (int)$this->post['isrec'];
            $city = $this->post['city'];
            !$city && msg("出发城市不能为空");
            $pics = '';
            if($_FILES['imgurl']['name']){
            	$imgname=strtolower($_FILES['imgurl']['name']);
            	$type = substr(strrchr($imgname,'.'),1);
            	if(!isimage($type)) {
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！");
	            }
	            $pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/lineimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/lineimg');
	            move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath);
            }
            $lid = $_ENV['line']->add($uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec,$idcode,$city, $cateid);
            //$this->cache->remove('line');
            //设置线路系统分类
            $_ENV['sysmap']->dellinecate($lid);
            foreach ($topid as $tk => $tv) {
            	$_ENV['sysmap']->inlinecate($lid,$cate_{$tk});
            }
            //设置景点归属
            //$_ENV['sysmap']->dellineview($lid);
            //$_ENV['sysmap']->inlineview($lid,$views);
            //设置线路归属分站
            //$_ENV['sysmap']->upline_site($uid,$lid,$siteid);
            //设置线路归属地区分类
            //$_ENV['sysmap']->upline_area($uid,$lid,$siteid,$fenleiid,$leibieid,$pifaarea);
            //$_ENV['sysmap']->uplineindex($uid,$lid);
            //$this->ondefault('添加成功！');
            $_ENV['sysmap']->upline_site_cate($uid,$lid,$cates);
            msg("添加成功，跳转到第二步","index.php?line/pifalineprice/$lid");
        }else{
        	$navtitle = '添加线路';
	    	
	    	$catestr = $_ENV['cate']->get_cateshow_user();//分类
	    	$cateconfig = $this->cateconfig;
	    	$catelist = $_ENV['line']->getcate($uid);
	    	//$pifasite = $_ENV['sysmap']->get_pifa_site($uid);
	    	//$pifaarea = $_ENV['sysmap']->get_pifa_area($uid);
	    	$pifasite = $_ENV['sysmap']->get_user_area_pifa($uid);
	        include template('pifaline');
        }
    	
    }
    function onpifalineedit(){
  		$uid = $this->user['uid'];
		if(isset($this->post['uid'])) {
			$lid = (int)$this->post['lid'];
            $idcode = $this->post['idcode'];
            $title = $this->post['title'];
            $topid = $this->post['topid'];
            foreach ($topid as $tk => $tv) {
            	$cate_{$tk} = $this->post["cate_$tk"];
            	if(count($cate_{$tk}) ==0){
            		msg($tv."不能为空");
            	}
            }
            /*
            $siteid = (int)$this->post['siteid'];
            $siteid == 0 && msg("请选择分站");
            $fenleiid = (int)$this->post['fenleiid'];
            $fenleiid == 0 && msg("请选择分类");
            $leibieid = (int)$this->post['leibieid'];
            $leibieid == 0 && msg("请选择类别");
            //$pifasite = $this->post['pifasite'];
            //count($pifasite) ==0 && msg("请选择发布的分站");
            $pifaarea = $this->post['pifaarea'];
            count($pifaarea) ==0 && msg("请选择发布到的地区");
            */
            $cates = $this->post['cates'];
            count($cates) ==0 && msg("请选择发布到的地区");
            !$title && msg("线路名称不能为空");
            //$cateconfig = (int)$this->post['cateconfig'];
            //$cateconfig == 0 && msg("请选择线路类别");
            //$views = $this->post['viewshow'];
            //count($views) ==0 && msg("请选择途经景点");
            $earlier_date = (int)$this->post['earlier_date'];
            $earlier_date == 0 && msg("提前报名的天数不能为空");
            $daynum = (int)$this->post['daynum'];
            $daynum == 0 && msg("行程天数不能为空");
            $group_date = (int)$this->post['group_date'];
            $traffic = $this->post['traffic'];
            !$traffic && msg("交通方式不能为空");
            $orderid = (int)$this->post['orderid'];
            $isrec = (int)$this->post['isrec'];
            $city = $this->post['city'];
            !$city && msg("出发城市不能为空");
            $pics = '';
            if($_FILES['imgurl']['name']){
            	$imgname=strtolower($_FILES['imgurl']['name']);
            	$type = substr(strrchr($imgname,'.'),1);
            	if(!isimage($type)) {
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！");
	            }
	            $pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/lineimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/lineimg');
	            move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath);
            }
            //             update($lid,$uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec,$idcode='',$city='')
            $_ENV['line']->update($lid,$uid,$title,$earlier_date,$pid,$group_date,$daynum,$traffic,$pics,$orderid,$isrec,$idcode,$city);
            //$this->cache->remove('line');
            //设置线路系统分类
            $_ENV['sysmap']->dellinecate($lid);
            foreach ($topid as $tk => $tv) {
            	$_ENV['sysmap']->inlinecate($lid,$cate_{$tk});
            }
            //设置景点归属
            //$_ENV['sysmap']->dellineview($lid);
            //$_ENV['sysmap']->inlineview($lid,$views);
            //设置线路归属分站
            //$_ENV['sysmap']->upline_site($uid,$lid,$siteid);
            //设置线路归属地区分类
            //$_ENV['sysmap']->upline_area($uid,$lid,$siteid,$fenleiid,$leibieid,$pifaarea);
            //$_ENV['sysmap']->uplineindex($uid,$lid);
            //$this->ondefault('添加成功！');
            $_ENV['sysmap']->upline_site_cate($uid,$lid,$cates);
            msg("添加成功，跳转到第二步","index.php?line/pifalineprice/$lid");
        }else{
        	$navtitle = '编辑线路';
	    	$lid = intval($this->get[2]);
            $d = $_ENV['line']->get($lid);
	    	$catestr = $_ENV['cate']->get_cateshow_user($lid);//分类
	    	$cateconfig = $this->cateconfig;
	    	$catelist = $_ENV['line']->getcate($uid);
	    	//$pifasite = $_ENV['sysmap']->get_pifa_site($uid);
	    	//$pifaarea = $_ENV['sysmap']->get_pifa_area($uid);
	    	$pifasite = $_ENV['sysmap']->get_user_area_pifa($uid,$lid);
	        include template('pifalineedit');
        }  	
    }
    function onpifalineprice() {
    	$lid = $this->get[2];
    	!$lid && msg('非法操作！');
    	$lineinfo = $_ENV['line']->get($lid);
    	if (!$lineinfo['id']) {
    		msg('非法操作！');
    	}
    	$lineprice = $_ENV['line']->get_prciebylid($lid);
    	$pricelist = $_ENV['line']->getpricelist($lid);
		if (isset($this->post['submit'])) {
			$servicestand = $this->post['servicestand'];
			$price_stand = $this->post['price_stand'];
			$price_seller = $this->post['price_seller'];
			$price_child_stand = $this->post['price_child_stand'];
			$price_child_seller = $this->post['price_child_seller'];
			$senddate = $this->post['senddate'];
			$senddate = $this->post['senddate'];
			$descs = $this->post['descs'];
			$isdef = $this->post['isdef'];
			$id = $this->post['id'];
			$isdef = $this->post['isdef'];
			foreach ($servicestand as $key=>$service) {
				$data = array(
					'lid' => $lid,
					'price_stand' => $price_stand[$key],
					'price_seller' => $price_seller[$key],
					'price_child_stand' => $price_child_stand[$key],
					'price_child_seller' => $price_child_seller[$key],
					'descs' => $descs[$key],
					'isdef' => $isdef[$key] ? 1 : 0,
					'servicestand' => $service,
					'senddate' => $senddate[$key],
				);
				if (($data['price_stand'] != '同业价' && !empty($data['price_stand'])) && ($data['price_seller'] != '销售价' && !empty($data['price_seller'])) && ($data['price_child_stand'] != '同业价' && !empty($data['price_child_stand'])) && ($data['price_child_seller'] != '销售价' && !empty($data['price_child_seller']))) {
					if ($id[$key]) {
						$_ENV['line']->upprice($data, $id[$key]);
					} else {
						
						$_ENV['line']->addprice($data);
					}
				}
			}
			msg('操作成功！', '?line/pifaeditday/'.$lid);
		} else {
			include template('pifalineprice');
		}
	}
	function oneditpricesave() {
		if(isset($this->post['lid'])) {
			$typename = $this->post['typename'];
			!$typename && msg("价格类型不能为空") ;
			$price = (int)$this->post['price'];
			$price_my = (int)$this->post['price_my'];
			!$price_my && msg("优惠价不能为空") ;
			$price_child = (int)$this->post['price_child'];
			$descs = $this->post['descs'];
			!$descs && msg("费用说明不能为空") ;
			$views = (int)$this->post['views'];
			$lid = (int)$this->post['lid'];
			$_ENV['line']->addprice($lid,$typename,$price,$price_my,$price_child,$descs,$views);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("添加成功","index.php?line/pifalineprice/$lid");
			}else{
				msg("添加成功","index.php?line/pifaeditday/$lid");
			}
	        
		}else{
			msg("error!");
		}
		
	}
	function oneditpriceedit() {
		if(isset($this->post['pid'])) {
			$typename = $this->post['typename'];
			!$typename && msg("价格类型不能为空") ;
			$price = (int)$this->post['price'];
			$price_my = (int)$this->post['price_my'];
			!$price_my && msg("优惠价不能为空") ;
			$price_child = (int)$this->post['price_child'];
			$descs = $this->post['descs'];
			!$descs && msg("费用说明不能为空") ;
			$views = (int)$this->post['views'];
			$lid = (int)$this->post['lid'];
			$pid = (int)$this->post['pid'];
			$_ENV['line']->upprice($pid,$lid,$typename,$price,$price_my,$price_child,$descs,$views);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?line/pifalineprice/$lid");
			}else{
				msg("编辑成功","index.php?line/pifaeditday/$lid");
			}
		}else{
			msg("error!");
		}
		
	}
	function ondelprice() {
    	$_ENV['line']->del_price(intval($this->get[3]));
        msg("删除成功","index.php?line/pifalineprice/".intval($this->get[2]));
    }
	function onpifaeditday()
	{
		if(isset($this->post['lid'])) {
			$lid = intval($this->post['lid']);
			$descs = $this->post['features'];
			$type =  intval($this->post['sch_tc']);
			$content=$this->post['content'];
			if($type == 1) {
				$att=$this->post['att'];
				$_ENV['line']->updays($lid,$att);
			}
			$_ENV['line']->upday($lid,$descs,$type,$content);
			$tz = (int)$this->post['tz'];
			if($tz == 1) {
				msg("编辑成功","index.php?line/pifaeditday/$lid");
			}else{
				msg("编辑成功","index.php?line/pifaeditdesc/$lid");
			}
			
		}else{
			$lid = intval($this->get[2]);
			$d = $_ENV['line']->get($lid);
			$ds = $_ENV['line']->get_day($lid);
			$list = $_ENV['line']->get_days($lid);
			$dstr = $_ENV['line']->get_dayshow($lid,$d[daynum]);
			include template('pifalineday');
		}
		
	}
	function onpifaeditdesc() {
		if(isset($this->post['lid'])) {
			$lid = intval($this->post['lid']);
			$info = $_ENV['line']->get_descs($lid);
			if (isset($info['lid'])) {
				$data = $this->post['data'];
				$_ENV['line']->updesc($data, "lid='$lid'");
			} else {
				$data = $this->post['data'];
				$data['lid'] = $lid;
				$_ENV['line']->adddesc($data);
			}
			msg('操作成功！');
		}else{
			$lid = intval($this->get[2]);
			$info = $_ENV['line']->get_descs($lid);
			include template('pifalinedesc');
		}
	}
	
	
    function onajaxgetleibie() {
    	$siteid = intval($this->get[2]);
    	$userid = intval($this->get[3]);
    	$cateconfig = $this->cateconfig;
    	$str = '';
    	$mdb = $this->db->query ( "SELECT distinct k2 FROM my_mapuserarea WHERE k1 = '$siteid' and uid = '$userid'" );
    	if($this->db->num_rows($mdb)>0){
    		$str .= '<DT>线路类别：<EM>*</EM></DT><DD><select name=leibieid onchange="chg_leibie()" id="leibieid"><option value="0">请选择线路类别</option>';
    		while ( $m = $this->db->fetch_array ( $mdb ) ) {			
				$str .= '<option value="' . $m[k2] . '">' . $cateconfig[$m[k2]][name] . '</option>';
			}
			$str .= '</select><SPAN><font color="bule">请正确选择线路类别，必选</font></SPAN></DD>';
			echo $str;
    	}else{
    		echo '<dt>&nbsp;</dt><dd><font color="bule">您没有权限在该分站发布线路，请联系管理员</font></dd>';
    	}
		
    	
    }
    function onajaxgetfenlei() {
    	$cateid = intval($this->get[2]);
    	$userid = intval($this->get[3]);
    	$cate = $this->cate;
    	$str = '';
    	$mdb = $this->db->query ( "SELECT distinct k3 FROM my_mapuserarea WHERE k2 = '$cateid' and uid = '$userid'" );
    	if($this->db->num_rows($mdb)>0){
    		$str .= '<DT>线路类型：<EM>*</EM></DT><DD><select name=fenleiid onchange="chg_diqu()" id="fenleiid"><option value="0">请选择线路类型</option>';
    		while ( $m = $this->db->fetch_array ( $mdb ) ) {			
				$str .= '<option value="' . $m[k3] . '">' . $cate[$m[k3]][name] . '</option>';
			}
			$str .= '</select><SPAN><font color="bule">请正确选择线路类型，必选</font></SPAN></DD>';
			echo $str;
    	}else{
    		echo '<dt>&nbsp;</dt><dd><font color="bule">您没有权限在该分站发布线路，请联系管理员</font></dd>';
    	}
    }
    function onajaxgetdiqu() {
    	$cateid = intval($this->get[2]);
    	$userid = intval($this->get[3]);
    	//$cate = $this->cate;
    	$str = '';
    	$mdb = $this->db->query ( "SELECT distinct aid FROM my_mapuserarea WHERE k3 = '$cateid' and uid = '$userid'" );
    	if($this->db->num_rows($mdb)>0){
    		$str .= '<dl><DT>线路地区：<EM>*</EM></DT><DD>';
    		while ( $m = $this->db->fetch_array ( $mdb ) ) {
    			$aname = $_ENV['area']->getname_byid($m[aid]);
				//$str .= '<option value="' . $m[aid] . '">' . $aname . '</option>';
				//$str .= '<input type="checkbox" name="pifaarea[' . $m ['aid'] . ']" value="' . $m ['aid'] . '" onclick="checkview(this);">' . $aname . '&nbsp;&nbsp;';
				$str .= '<input type="checkbox" name="pifaarea[' . $m ['aid'] . ']" value="' . $m ['aid'] . '">' . $aname . '&nbsp;&nbsp;';
			}
			$str .= '<SPAN><font color="bule">请正确选择线路地区，必选</font></SPAN></DD></dl>';
			/*
			$str .= '<dl ><DT>途经景点：<EM>*</EM></DT><DD id="jingdian">';
			$mtb = $this->db->query ( "SELECT distinct aid FROM my_mapuserarea WHERE k3 = '$cateid' and uid = '$userid'" );
			while ( $mt = $this->db->fetch_array ( $mtb ) ) {
				$str .= '<span id="view_' . $mt['aid'] . '"></span>';
			}
			$str .= '<SPAN><font color="bule">必选</font></SPAN></DD></dl>';
			*/
			echo $str;
    	}else{
    		echo '<dl><dt>&nbsp;</dt><dd><font color="bule">您没有权限在该分站发布线路，请联系管理员</font></dd></dl>';
    	}
    }
    
    function onajaxshwowviews() {
    	$areaid = intval($this->get[2]);
    	$userid = intval($this->get[3]);
    	$lineid = intval($this->get[4]);
    	$larray = array ();
		if ($lineid>0) {
			$lq = $this->db->query ( "SELECT `vid` FROM `" . DB_TABLEPRE . "maplineview` where `lid`='$lid'" );
			while ( $c = $this->db->fetch_array ( $lq ) ) {
				$larray [] = $c ['vid'];
			}
		}
		
		$str = '';
		$mdb = $this->db->query ( "SELECT id,sid,name,sname FROM `" . DB_TABLEPRE . "views` WHERE sid = '$areaid'" );
		if($this->db->num_rows($mdb)>0){
			//$str .= '<DT>线路景点：<EM>*</EM></DT><DD>';
			while ( $m = $this->db->fetch_array ( $mdb ) ) {
				if (empty ( $m ['sname'] )) {
					$name = $m ['name'];
				} else {
					$name = $m ['sname'];
				}
				if (in_array ( $m ['id'], $larray )) {
					$ck = ' checked';
				} else {
					$ck = '';
				}
				$str .= '<input type="checkbox" name="viewshow[' . $m ['id'] . ']" value="' . $m ['id'] . '"' . $ck . '>' . $name . '&nbsp;&nbsp;';
			}
			//$str .= '<SPAN><font color="bule">请正确选择线路景点，必选</font></SPAN></DD>';
			echo $areaid.'@'.$str;
		}

    }
}

?>